//
//  SigninVC.m
//  PStoneSDKDemo
//
//  Created by 元潮 谢 on 16/10/13.
//  Copyright © 2016年 元潮 谢. All rights reserved.
//

#import "SigninVC.h"
#import "FoundBluetoothVC.h"
#import "SettingUtils.h"

@interface SigninVC ()<UITextFieldDelegate>
{
    
    IBOutlet UITextField * mTextFieldAccount;
    IBOutlet UITextField * mTextFieldPs;
    
}
@end

@implementation SigninVC

-(IBAction)signIn:(id)sender
{

    if ([mTextFieldPs.text isEqualToString:@"7ujmko0"]) {
        
        [SettingUtils setAccount:mTextFieldAccount.text];
        [SettingUtils setPassword:mTextFieldPs.text];
        [SettingUtils setSignUp:YES];
        
        for (UIViewController * vc in self.navigationController.viewControllers) {
            
            if ([vc isKindOfClass:[FoundBluetoothVC class]]) {
                
                [self.navigationController popToViewController:vc animated:YES];
                return;
            }
        }
        
        FoundBluetoothVC * vc = [[FoundBluetoothVC alloc]initWithNibName:@"FoundBluetoothVC" bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else{
    
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"用户名密码错误，请重新登录。" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"登陆";
        
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    
    mTextFieldAccount.text = [SettingUtils getAccount];
    
    if ([SettingUtils getSignUp]) {
        
        self.navigationItem.hidesBackButton = NO;
    }else{
        
        self.navigationItem.hidesBackButton = YES;
    }
}

-(void)viewDidDisappear:(BOOL)animated
{
    
    [super viewDidDisappear:YES];
    [self removeFromParentViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    if ([textField isEqual:mTextFieldAccount]) {
        
        [mTextFieldPs becomeFirstResponder];
    }else{
        
        [self signIn:textField];
    }
    return YES;
}

@end
